import '../../util/_base_export.dart';
import '../../util/_view_export.dart';
import 'package:clipboard/clipboard.dart';

class ReferalMainScreen extends StatefulWidget {
  const ReferalMainScreen({Key? key}) : super(key: key);
  @override
  _ReferalMainScreenState createState() => _ReferalMainScreenState();
}

class _ReferalMainScreenState extends State<ReferalMainScreen> {
  @override
  void initState() {
    super.initState();
  }

  _login(BuildContext context) {
    App.router.navigateTo(context, AppRoutes.loginSignin);
  }

  _cancel() {}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   brightness: Brightness.light,
      //   title: Text('选择区域', style: TextStyle(color: AppColor.blackMain)),
      // ),
      body: SingleChildScrollView(
        child: Container(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              width: 375.w,
              height: 358.h,
              decoration: new BoxDecoration(
                image: new DecorationImage(
                  image: new AssetImage("assets/referal/header.png"),
                  fit: BoxFit.cover,
                ),
              ),
              child: Padding(
                padding: EdgeInsets.only(left: 187.w, top: 114.h, right: 35.w),
                child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
                  Text(
                    '邀请好友',
                    style: AppTextStyle.header1Referal,
                  ),
                  Text(
                    '双双有礼',
                    style: AppTextStyle.header1Referal,
                  ),
                  Padding(
                    padding: EdgeInsets.only(
                      top: 20.h,
                    ),
                    child: Text(
                      '分享邀请码给好友，被邀请好友成功注册新用户后，您可以获得\$10抵用券，该新用户总共可以获得\$20抵用券。多邀多得，上不封顶。',
                      style: AppTextStyle.header2Referal,
                      softWrap: true,
                      overflow: TextOverflow.fade,
                      maxLines: 9,
                    ),
                  ),
                ]),
              ),
            ),
            Container(
              height: 120.h,
              // padding: EdgeInsets.symmetric(horizontal: 100.w, vertical: 30.h) ,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Text(
                    '我的邀请码',
                    style: AppTextStyle.code1Referal,
                  ),
                  Text(
                    AppSession.appUtil.data['referral_code'] ?? '',
                    style: AppTextStyle.code2Referal,
                  ),
                ],
              ),
            ),
            Container(
              height: 400.h,
              // padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 25.h) ,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () {
                      if ((AppSession.authortoken ?? '').isEmpty)
                        AppPopupService.showAlertSimple(
                          'I',
                          '请先登录',
                          '',
                          iv_cancel_text: '先不了',
                          cancel: _cancel,
                          iv_confirm_text: '去登录',
                          confirm: _login(context),
                        );
                      else {
                        FlutterClipboard.copy(AppSession.appUtil.data['referral_code']).then(
                          (value) => AppPopup.popupToast('I', '已复制，快去分享'),
                        );
                      }
                    },
                    child: Image.asset('assets/referal/apply_icon_btn.png'),
                  ),
                  TextButton(
                      child: Text(
                        '已收到邀请码, 点击输入 >',
                        style: AppTextStyle.code1Referal,
                      ),
                      onPressed: () {
                        if ((AppSession.authortoken ?? '').isEmpty)
                          AppPopupService.showAlertSimple(
                            'I',
                            '请先登录',
                            '',
                            iv_cancel_text: '先不了',
                            cancel: _cancel,
                            iv_confirm_text: '去登录',
                            confirm: _login(context),
                          );
                        else {
                          App.router.navigateTo(context, AppRoutes.accountRedeem);
                        }
                      })
                ],
              ),
            ),
            _cupBlock(context),
            _rankBlock(context),
          ],
        )),
      ),
    );
  }

  Widget _cupBlock(BuildContext context) {
    return Container(
      height: 350.h,
      padding: EdgeInsets.symmetric(horizontal: 26.w, vertical: 20.h),
      child: Stack(children: <Widget>[
        Align(
          alignment: Alignment.bottomCenter,
          child: Container(
            height: 190.h,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(15)),
              gradient: LinearGradient(
                colors: [Color(0xFFFF3434), Color(0xFFF77631)],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
              ),
            ),
            padding: EdgeInsets.symmetric(horizontal: 30.w),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Padding(
                  padding: EdgeInsets.only(top: 30.h),
                  child: Text('排行榜前三名可获相应抵用券，榜单每周刷新', style: AppTextStyle.cup1Referal),
                ),
                Row(
                  // mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: Container(
                        height: 75.h,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            Text(
                              '第2名',
                              style: AppTextStyle.cup2Referal,
                            ),
                            ((AppSession.appUtil.referral_rank['setting'] ?? {}).isNotEmpty)
                                ? Text(
                                    '\$${AppSession.appUtil.referral_rank['setting']['2'] ?? ''}',
                                    style: AppTextStyle.cup2Referal,
                                  )
                                : Text(''),
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Container(
                        height: 75.h,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            Text(
                              '第1名',
                              style: AppTextStyle.cup3Referal,
                            ),
                            ((AppSession.appUtil.referral_rank['setting'] ?? {}).isNotEmpty)
                                ? Text(
                                    '\$${AppSession.appUtil.referral_rank['setting']['1'] ?? ''}',
                                    style: AppTextStyle.cup3Referal,
                                  )
                                : Text(''),
                          ],
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Container(
                        height: 75.h,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            Text(
                              '第3名',
                              style: AppTextStyle.cup2Referal,
                            ),
                            ((AppSession.appUtil.referral_rank['setting'] ?? {}).isNotEmpty)
                                ? Text(
                                    '\$${AppSession.appUtil.referral_rank['setting']['3'] ?? ''}',
                                    style: AppTextStyle.cup2Referal,
                                  )
                                : Text(''),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Image.asset('assets/referal/rank_all.png'),
        ),
      ]),
    );
  }

  Widget _rankBlock(BuildContext context) {
    print(AppSession.appUtil.referral_rank['list']);
    List<Widget> ll_widget = [];
    ll_widget.add(Text('赚钱榜', style: AppTextStyle.rankHeaderReferal));
    ll_widget.add(Padding(
      padding: EdgeInsets.symmetric(vertical: 5.h),
      child: Text('邀请人数相同者按完成时间先后顺序', style: AppTextStyle.rankSubHeaderReferal),
    ));
    ll_widget.add(Divider(
      color: Colors.grey,
      height: 1,
      thickness: 1,
      indent: 20,
      endIndent: 20,
    ));
    if ((AppSession.appUtil.referral_rank['list'] ?? []).isNotEmpty)
      AppSession.appUtil.referral_rank['list'].asMap().forEach((index, entry) {
        print(entry);
        ll_widget.add(_lineItem(index, entry));
      });
    ll_widget.add(Divider(
      color: Colors.grey,
      height: 1,
      thickness: 1,
      indent: 20,
      endIndent: 20,
    ));
    ll_widget.add(Padding(
      padding: EdgeInsets.symmetric(vertical: 15.h),
      child: Text('No. ${AppSession.appUtil.referral_rank['rank']}',
          style: AppTextStyle.rankTextReferal),
    ));
    ll_widget.add(Padding(
      padding: EdgeInsets.symmetric(vertical: 15.h),
      child: Text('每周一早上刷新排行榜，发放奖励', style: AppTextStyle.rankTextReferal),
    ));
    return Column(
      children: ll_widget,
      //  <Widget>[
      // Text('赚钱榜', style: AppTextStyle.rankHeaderReferal),
      // Padding(
      //   padding: EdgeInsets.symmetric(vertical: 5.h),
      //   child: Text('邀请人数相同者按完成时间先后顺序', style: AppTextStyle.rankSubHeaderReferal),
      // ),
      // Divider(
      //   color: Colors.grey,
      //   height: 1,
      //   thickness: 1,
      //   indent: 20,
      //   endIndent: 20,
      // ),
      // if ((AppSession.appUtil.referal_rank['list'] ?? []).isNotEmpty)
      //   AppSession.appUtil.referal_rank['list'].asMap().forEach((index, entry) {
      //     print(entry);
      //     return _lineItem(index, entry);
      //   }),
      // Divider(
      //   color: Colors.grey,
      //   height: 1,
      //   thickness: 1,
      //   indent: 20,
      //   endIndent: 20,
      // ),
      // Padding(
      //   padding: EdgeInsets.symmetric(vertical: 5.h),
      //   child: Text('No. ${AppSession.appUtil.referal_rank['rank']}',
      //       style: AppTextStyle.rankTextReferal),
      // ),
      // ]
    );
  }

  static Widget _lineItem(int lv_index, Map lm_entry) {
    //(Map item, TextStyle iv_text_style) {
    return Container(
      // height: 50.h,
      margin: EdgeInsets.symmetric(
        vertical: 12.h,
        horizontal: 26.w,
      ),
      // decoration: AppBoxDecoration.defaultList,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Row(
            children: <Widget>[
              if (lv_index == 0) Image.asset('assets/referal/rank_icon_gold.png'),
              if (lv_index == 1) Image.asset('assets/referal/rank_icon_silver.png'),
              if (lv_index == 2) Image.asset('assets/referal/rank_icon_bronze.png'),
              Text(lm_entry['user_name'], style: AppTextStyle.rankTextReferal),
            ],
          ),
          Text('邀请了 ${lm_entry['score']} 人', style: AppTextStyle.rankTextReferal),
        ],
      ),
    );
  }
}
